package com.ruoyi.iot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.iot.domain.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface IotSensorMapper extends BaseMapper<IotSensor> {
    @Select({
        "<script>",
        "SELECT s.*, st.*, dvc.*",
        "FROM iot_sensor s ",
        "left JOIN iot_station st ON s.station_id = st.station_id ",
        "left JOIN iot_device dvc ON s.device_id = dvc.remote_id",
        "<where>",
        // station_name 为空时不添加此条件
        "<if test='stationName != null and stationName != \"\"'>",
        "st.station_name LIKE CONCAT('%', #{stationName}, '%')",
        "</if>",
        // sensor_name 为空时不添加此条件
        "<if test='sensorName != null and sensorName != \"\"'>",
        "AND s.sensor_name LIKE CONCAT('%', #{sensorName}, '%')",
        "</if>",
        // sensor_id 为空时不添加此条件
        "<if test='sensorId != null and sensorId != \"\"'>",
        "AND s.sensor_id LIKE CONCAT('%', #{sensorId}, '%')",
        "</if>",
        // station_id 为空时不添加此条件
        "<if test='stationId != null and stationId != \"\"'>",
        "AND s.station_id LIKE CONCAT('%', #{stationId}, '%')",
        "</if>",
        // sensor_type 为空时不添加此条件
        "<if test='sensorType != null and sensorType != \"\"'>",
        "AND s.sensor_type LIKE CONCAT('%', #{sensorType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    Page<IotSensor> findSensorsWithStationName(
        Page<IotSensor> iPage,
        @Param("stationName") String stationName,
        @Param("sensorName") String sensorName,
        @Param("sensorId") String sensorId,
        @Param("stationId") String stationId,
        @Param("sensorType") String sensorType
    );


    @Select({
        "<script>",
        "SELECT * FROM ${tableName} WHERE device_id = #{deviceId}",
        "</script>"
    })
    List<Map<String, Object>> getDataByTableNameAndId(
        @Param("tableName") String tableName,
        @Param("deviceId") String deviceId
    );

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_flow data on data.device_id = s.sensor_id\n",        // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataFlow> getFlowDataList(String stationType);

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_gnss data on data.device_id = s.sensor_id\n",        // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataGnss> getGnssDataList(String stationType);

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_rain data on data.device_id = s.sensor_id\n" ,       // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataRain> getRainDataList(String stationType);

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_termite data on data.device_id = s.sensor_id\n" ,       // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataTermite> getTermiteDataList(String stationType);
    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "left join iot_data_termite data on data.device_id = s.sensor_id\n" ,       // sensor_type 为空时不添加此条件
        "<where>",
        "s.sensor_type = 'termite_data'",
        "<if test='stationType != null and stationType != \"\"'>",
        "and st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "<if test='stationId != null and stationId != \"\"'>",
        "and st.station_id LIKE CONCAT('%', #{stationId}, '%')",
        "</if>",
        "<if test='sensorId != null and sensorId != \"\"'>",
        "and s.station_id LIKE CONCAT('%', #{sensorId}, '%')",
        "</if>",
        "<if test='remoteId != null and remoteId != \"\"'>",
        "and s.remoteId LIKE CONCAT('%', #{remoteId}, '%')",
        "</if>",
        "<if test='SensorType != null and SensorType != \"\"'>",
        "and s.sensor_type LIKE CONCAT('%', #{SensorType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    Page<IotDataTermite> getTermiteDataPage(Page<IotDataTermite> iPage,@Param("stationType") String stationType,
                                            @Param("stationId") String stationId,
                                            @Param("sensorId")String sensorId,
                                            @Param("remoteId") String remoteId,
                                            @Param("SensorType")String SensorType);

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_water_height data on data.device_id = s.sensor_id\n",        // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataWaterHeight> getWaterHeightDataList(String stationType);

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_pressure data on data.device_id = s.sensor_id\n"   ,     // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataWaterPressure> getWaterPressureDataList(String stationType);

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_water_quality data on data.device_id = s.sensor_id\n" ,       // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataWaterQuality> getWaterQualityDataList(String stationType);

    @Select({
        "<script>",
        "SELECT * FROM  iot_sensor s right JOIN iot_station st ON s.station_id = st.station_id \n",
        "inner join iot_data_wind data on data.device_id = s.sensor_id\n",        // sensor_type 为空时不添加此条件
        "<where>",
        "<if test='stationType != null and stationType != \"\"'>",
        "st.station_type LIKE CONCAT('%', #{stationType}, '%')",
        "</if>",
        "</where>",
        "</script>"
    })
    List<IotDataWind> getWindDataList(String stationType);


    @Select({
            "<script>",
            "SELECT * FROM iot_sensor",
            "WHERE device_id = #{deviceId}",
            "AND FIND_IN_SET(#{funcCode}, param_one)",
            "</script>"
    })
    List<IotSensor> selectByDeviceIdAndParamOne(@Param("deviceId")String deviceId, @Param("funcCode")String funcCode);
}
